<template>
  <div>
    <h2 align="center">借款信息修改页面</h2>
    <table class="table table-bordered">
      <tr>
        <td>借款款项名称:</td>
        <td>
          <input type="text" v-model="UpBorrowData.B_Name">
        </td>
      </tr>

      <tr>
        <td>借款最高金额:</td>
        <td>
          <input type="text" v-model="UpBorrowData.B_Money">
        </td>
      </tr>

      <tr>
        <td>借款最高年限:</td>
        <td>
          <input type="text" v-model="UpBorrowData.B_Year">
        </td>
      </tr>

      <tr>
        <td>利息:</td>
        <td>
          <select v-model="UpBorrowData.BI_Id">
            <option value="0">==请选择==</option>
            <option v-for="item in list" :value="item.BI_Id">
                {{item.BI_Year}}年{{(item.BI_Number)}}
            </option>
          </select>
        </td>
      </tr>

      <tr>
        <td>借款款项简介:</td>
        <td>
          <textarea rows="2" cols="22" v-model="UpBorrowData.B_Show">

          </textarea>
        </td>
      </tr>

      <tr>
        <td></td>
        <td>
          <input type="button" @click="UpBorrowInfo" value="确认修改" class="btn btn-primary">
        </td>
      </tr>
    </table>
  </div>
</template>

<script setup lang="ts">

import {onMounted, reactive,toRefs,watch} from 'vue';

import { useRouter,useRoute } from 'vue-router';

import axios from 'axios';

//路由
let router = useRouter();
let route = useRoute()

//响应式数据
let data:any =reactive({
  list:[],
  //修改理财信息
   UpBorrowData:{
    B_Name:'',
    B_Year:'',
    B_Money:'',
    B_Show:'',
    BI_Id:0,
    B_Id:route.query.B_Id
   }
})

//解构
let {list,UpBorrowData} = toRefs(data);

//生命周期
//页面加载时
onMounted(()=>{
  getList()
  BackBorrow()
})

//获取利息表
const getList =()=>{
  axios({
    url:'/api/Borrow/GetBorrowInterest',
    method:'get',
  }).then(res=>{
    console.log(res);
    list.value=res.data.data
  }).catch(err=>{
    console.log(err);
    
  })
}

//反填
const BackBorrow = ()=>{
  axios({
    url:'/api/Borrow/BackInfo',
    method:'get',
    params:{
      B_Id:route.query.B_Id
    }
  }).then(res=>{
    console.log(res);
    UpBorrowData.value=res.data.data[0]
  }).catch(err=>{
    console.log(err);
    
  })
}



//确认修改
const UpBorrowInfo =()=>{
  //非空判断
  if(UpBorrowData.value.B_Name==""){
    return alert('借款款项名称不能为空')
  }
  if(UpBorrowData.value.B_Money==""){
    return alert('借款最高金额不能为空')
  }
  if(isNaN(UpBorrowData.value.B_Money)){
    return alert('借款最高金额必须为数字') 
  }
  if(UpBorrowData.value.B_Money<0 || UpBorrowData.value.B_Money>1000000){
    return alert('借款最高金额必须在0到1000000之间')
  }
  if(UpBorrowData.value.B_Year==""){
    return alert('借款最高年限不能为空')
  }
  if(isNaN(UpBorrowData.value.B_Year)){
    return alert('借款最高年限必须为数字') 
  }
  if(UpBorrowData.value.B_Year<1 || UpBorrowData.value.B_Year>10){
    return alert('借款最高年限必须在1到10之间')
  }
  if(UpBorrowData.value.B_Show==""){
    return alert('借款款项简介不能为空')
  }
  if(UpBorrowData.value.BI_Id==0){
    return alert('请选择利息')
  }
  //发送请求
  axios({
    url:'/api/Borrow/GetBorrowUpdate',
    method:'post',
    data:UpBorrowData.value
  }).then(res=>{
    console.log(res);
    if(res.data.data>0){
      alert('修改成功')
      router.push('/main/borrowTest')
    }else{
      return alert('修改失败')
    }
  }).catch(err=>{
    console.log(err);
    
  })
}



</script>

<style scoped>

</style>